/*
 * Copyright 2012 - 2017 Anton Tananaev (anton.tananaev@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tendory.gpssim.activity;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.tendory.gpssim.R;
import com.tendory.gpssim.evt.EvtStChanged;
import com.tendory.jt808.msg.CommandReply;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class StatusActivity extends AppCompatActivity {

    private static final int LIMIT = 20;

    private static final LinkedList<String> messages = new LinkedList<>();
    private static final Set<ArrayAdapter<String>> adapters = new HashSet<>();
    private TextView acc, alarm, block;

    private static void notifyAdapters() {
        for (ArrayAdapter<String> adapter : adapters) {
            adapter.notifyDataSetChanged();
        }
    }

    public static void addMessage(String message) {
        DateFormat format = DateFormat.getTimeInstance(DateFormat.MEDIUM);
        message = format.format(new Date()) + " - " + message;
        messages.add(message);
        while (messages.size() > LIMIT) {
            messages.removeFirst();
        }
        notifyAdapters();
    }

    public static void clearMessages() {
        messages.clear();
        notifyAdapters();
    }

    private ArrayAdapter<String> adapter;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        acc = findViewById(R.id.acc);
        alarm = findViewById(R.id.alarm);
        block = findViewById(R.id.block);

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, messages);
        ListView listView = findViewById(android.R.id.list);
        listView.setAdapter(adapter);
        adapters.add(adapter);

        EventBus.getDefault().register(this);
        checkSt();
        acc.setOnClickListener(v -> {
            boolean a = preferences.getBoolean("acc", false);
            preferences.edit().putBoolean("acc", !a).apply();
            checkSt();
            EventBus.getDefault().post(new EvtStChanged());
        });
        alarm.setOnClickListener(v -> {
            boolean b = preferences.getBoolean("alarm", false);
            preferences.edit().putBoolean("alarm", !b).apply();
            checkSt();
            EventBus.getDefault().post(new EvtStChanged());
        });
    }

    private void checkSt() {
        boolean a = preferences.getBoolean("acc", false);
        acc.setText(a ? "Acc On" : "Acc Off");
        boolean b = preferences.getBoolean("alarm", false);
        alarm.setText(b ? "Alarm On" : "Alarm Off");
        boolean c = preferences.getBoolean("block", false);
        block.setText(c ? "Block On" : "Block Off");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(CommandReply event) {
        checkSt();
    }

    @Override
    protected void onDestroy() {
        adapters.remove(adapter);
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.status, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.clear) {
            clearMessages();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
